package com.wwj.auction.login.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {
    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public static String app_id = "2021000116684033";
    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDA+HnzPriAFTLYuk/uHR1wPooA4FdkahthRa/rq/ZmzOgZ+Gn3HmET1qYy7DyOWeGIKCiad7Lpb/a6idRHtEADha2/NTQMgUC+cnPfFnliC97LPwl6imik84GO5XZ0yxNvqzHIurIXBEJegfSSm1mKuDU6DavGSQlRUKDSiLcL/EOKJyWfrcEeZWLy5TbZccdiurb7Gm5wWeaYUSm0qSpZaxrhhkB24UzFEc9DZZvhmtFyZ+WzeImofX9254pNIKXgeDpVO0e2KKXdcdPVpL0pzg/3RjPaZhPcgrOwVLum8Nj8haA0HjkJqbI5JzWU6XiKi889ihYeI/imETXz+wgDAgMBAAECggEAMKy8fhndChnOyJI+ZS6c5VoxD0WHZcHKBEBCvqySq52WEi7x866LCCkFpSSnpbSQSfrvOv9kdrOSRb1MeYXC++B1UhzGcz0LKr7N8YDGv2+FFDljV7Cf8rOlg3jn50/uKCXDmgFVuK6PWy1mlrBu5qnOa0VVhd+YZRV0a4amvXPFDfKisSxMI1+oIgqC9LmyMz5gDhahlFKmpI9EHN0zno/eycw6xo/aCXagAmPU4UMOck1o9S1PhRQvyRlWSll9xuGWiimUi/a1j61hNTkD6XLLnH6cewBeGHKr9mOKBT9PywEj1qeX50AHHaVbX1HlUnOLlRuldYMO+r/B7GKmkQKBgQDoExEuna52PsNbp0SmTid+lqOoVAr5oi5xYABiXa0VruOvR7ZEikWzk1uq4deTwzzWpqy6S1ZHw/j8wxQ1SweZy187W6cFFAbJ4hsZFLE7m95NMGTGmgP0M+WbmDElAQmrQV6cYOXl09rU2d0M1QAqMNItQlLtBwLaT4zk4PZO2wKBgQDU3WBBzvSSQP83OrzHv4um9BDvhLeX8aUOEGjNeGIcYcCe0YABVQVEF+q9rWR6e1C+XRnY50kh0KNYa8xHpglfk9RyvK+k5kBfywnnq2ArbeaDxm2TcjRMhkzWyeVMFgRWmAfzzNk7ZSJv8+rjgEJvETDENftEY8r2Q1VuE9SP+QKBgF583OMI6i5UkX+wk7/OVEAzHwD67tkCkjww5RcnosAQS71iyjAPyB88IPdrKjWMKSoQBIV5BPEFNkvbNVAZc0fHyMdHIrJrm/P4biYBCcQGSzVMPcuqi2+S7aozBddWG9SxMc8jmJbBcfh1l42Q75c992Tb+x1sXFn8R5Ysjbw/AoGAM/zVM31eEhI5wr1fudE4RFA7iqP5SuzDiQnM7tHF9HSTXLaB8tFW9eh5dlXQwnE3qSaH14e1PsL8Ae5h9+W/SE6MhnE63KU3d63Y/3HFjRXMRMbDoE02JLJwNc0daVW/PX7oAk03iOAMZxdFeMascv/ZzzH36h4NRdPwG3BQa9ECgYEAxDcRdAHmpGFGenqqT8Z5eugHrSWz1bCSA9KrWZ7ymIsPfqk96/UpQDD/grB06uIrPahenFqOlWo9KEWl7+nHtPq6RzHHWgDcXKnjpeT4KxqmmTmJt3Vshb4z4znsHV1/wLuPtDC3hV8mDDoXVlpZ6ohgf3niFExQL4oQwT4Vtmo=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlt4bnL/TPec6FVf0+4v5SE+qYADh5iKPyLYhMXsKPyfETGkxK36fzOyp/lACX7AGLejGGjte7lE964GvfLXyuG0ecd6WWuLOepWZKlgi1XMiMzuZj/Gsb5aqvKPvi2/nWTlTUCceQ2sIpFtbnxQ29Aiq9b36qCIwHSCHH96SJRqT8DmiBTsGgAOEnJzy1knG/mPecD5jwIYP4NtLa0G6Px3EVz7ZFhZmR5LjxaqUpT/hIOG4Umjlkixf6g4Ga7rXXw8kzZBcSYTKq9LeBWqGGP1gt1dYv9m3ADAvd+wJn8U9T6veuawtExMSwyJkI/LzuzxqyaR8qB863C/3Mh+bEwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://localhost:3000/login/userAccount/notify";
    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String return_url = "http://localhost:3000/user"; // 支付完成后跳转回前端的账户总览页
    // 签名方式
    private String sign_type = "RSA2";
    // 字符编码格式
    private String charset = "utf-8";
    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"1m\"," // 1m（1分钟）时间内未付款窗口关闭
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;
    }
}
